package com.youaishujuhui.devops.web.sys;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.youaishujuhui.devops.web.domain.manage.SysUser;
import com.youaishujuhui.devops.web.exception.CustomException;
import com.youaishujuhui.devops.web.exception.ExceptionType;
import com.youaishujuhui.devops.web.service.manage.SysResourceService;
import com.youaishujuhui.devops.web.service.manage.SysUserService;
import com.youaishujuhui.devops.web.util.WebUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResourceHandlerInterceptor implements HandlerInterceptor {

	// @Autowired
	// private CacheManager cacheManager;
	
	@Autowired
	private SysResourceService sysResourceService;
	
	@Autowired
	private SysUserService sysUserService;

	private Logger log = LoggerFactory.getLogger(ResourceHandlerInterceptor.class);

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		SysUser loginUser = WebUtil.getLoginUser(request);
		// 判断用户是否登录登陆
		if (loginUser == null) {
			// response.sendRedirect(request.getContextPath() + "/");
			String url = request.getServletPath();
			String query = request.getQueryString();
			url = url + "?" + query;
			throw new CustomException(ExceptionType.LOGIN_FAIL).setExceptionData(url);
		}

		String reqUri = request.getServletPath();
		
		log.info("reqUri>>>>>>>>"+reqUri);
		
		// 系统配置的资源才需要拦截
		if (!sysResourceService.containsResourceUris(reqUri)) {
			return true;
		}
		
// 后续可以加缓存
//		if (!cacheManager.containsResourceUri(reqUri) && !cacheManager.containsReportUri(reqUri)) {
//			return true;
//		}
		
        //没有产品相关参数请求，说明请求的是系统管理，直接判断URI是否在权限范围
//	    if (loginUser.getResourceUris().contains(reqUri)) {
//	        return true;
//	    }
		
	    if (sysUserService.containsResourceUris(loginUser.getUserName(), reqUri)) {
	    	return true;
        }
		
		throw new CustomException(ExceptionType.AUTH_FAIL);

		
//		  String productId = request.getParameter(Common.PRODUCT_ID_PARAM_NAME);
//		  //log.info("productId>>>>>>>>"+productId); //String productCode =
//		  request.getParameter(Common.PRODUCT_CODE_PARAM_NAME);
//		  
//		  if(productId!=null){ 
//			  Map<String, Set<String>> productUriMap = loginUser.getProductIdUriMap(); 
//			  if (productUriMap != null && productUriMap.get(productId) != null && productUriMap.get(productId).contains(reqUri)) { 
//				  return true; 
//			  } 
//		  } else if(productCode!=null){ 
//			  Map<String, Set<String>> productUriMap = loginUser.getProductCodeUriMap(); 
//			  if (productUriMap != null && productUriMap.get(productCode) != null && productUriMap.get(productCode).contains(reqUri)) { 
//				  return true; 
//		      } 
//		  }
		 

	}

}
